package com.taobao.org.service.impl;

import com.taobao.basic.service.impl.BaseServiceImpl;
import com.taobao.basic.util.PageList;
import com.taobao.org.domain.Department;
import com.taobao.org.mapper.DepartmentMapper;
import com.taobao.org.query.DepartmentQuery;
import com.taobao.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * 新增数据
     * @param department
     */
    @Transactional
    @Override
    public void save(Department department) {
        // 先要返回自增长主键的id
        departmentMapper.save(department);
        // 上级部门之间关联操作 dirPath
        // 1.没有上级部门
        if(department.getParent_id() == null){
            Long id = department.getId();
            // 设置dirPath
            department.setDirPath("/" + id);
        }else { // 有上级部门
            // 通过parent_id 获取上级DirPath
            Long parent_id = department.getParent_id();
            // 父级部门对象
            Department parentDepartment = departmentMapper.loadById(parent_id);
            // 设置dirPath 拼接
            department.setDirPath(parentDepartment.getDirPath()+"/"+department.getId());
        }
        // 调用update方法，更新数据库
        update(department);
    }
    /**
     * 修改数据
     * @param department
     */
    @Transactional
    @Override
    public void update(Department department) {
        // 上级部门之间关联操作 dirPath
        // 1.没有上级部门
        if(department.getParent_id() == null){
            Long id = department.getId();
            // 设置dirPath
            department.setDirPath("/" + id );
        }else { // 有上级部门
            // 通过parent_id 获取上级DirPath
            Long parent_id = department.getParent_id();
            // 父级部门对象
            Department parentDepartment = departmentMapper.loadById(parent_id);
            // 设置dirPath 拼接
            department.setDirPath(parentDepartment.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }
    @Override
    public List<Department> deptTree() {
        // 1.查询所有
        List<Department> all = departmentMapper.loadAll();

        // 2.准备一个map
        HashMap<Long, Department> map = new HashMap<>();

        // 3.把所有数据放入map中
        for (Department department : all){
            map.put(department.getId(),department);
        }
        // 4.准备一个list,保存处理之后的数据
        ArrayList<Department> deptTree = new ArrayList<>();
        for (Department department : all){
            // 顶级部门
            if (department.getParent_id()==null){
                deptTree.add(department);
            }else {
                // 获取parent_id
                Long parent_id = department.getParent_id();
                // 获取上级部门对象
                Department partDept = map.get(parent_id);
                // 把自己放入上级部门的children中
                partDept.getChildren().add(department);
            }
        }
        // 返回部门树
        return deptTree;
    }
}
